﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace WebApp
{
    class ThreadBaseData
    {
        private static ThreadBaseData ins;
        public Hashtable Data
        {
            get;
            set;
        }
        private ThreadBaseData()
        {
            Data = new Hashtable();
        }
        public static ThreadBaseData Get()
        {
            if (ins == null)
            {
                ins = new ThreadBaseData();
            }
            return ins;
        }
        public void Clear()
        {
            lock (Data)
            {
                int id = Thread.CurrentThread.ManagedThreadId;
                if (Data.ContainsKey(id))
                {
                    ((Hashtable)Data[id]).Clear();
                }
            }
        }
        public object this[object Key]
        {
            get
            {
                lock (Data)
                {
                    int id = Thread.CurrentThread.ManagedThreadId;
                    if (Data.ContainsKey(id))
                    {
                        Hashtable d = (Hashtable)Data[id];
                        if (d.ContainsKey(Key))
                        {
                            return d[Key];
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        Hashtable d = new Hashtable();
                        Data.Add(Key, d);
                        return null;
                    }
                }
            }
            set
            {
                lock (Data)
                {
                    int id = Thread.CurrentThread.ManagedThreadId;
                    if (Data.ContainsKey(id))
                    {
                        Hashtable tdata = (Hashtable)Data[id];
                        if (tdata.ContainsKey(Key))
                        {
                            tdata[Key] = value;
                        }
                        else
                        {
                            tdata.Add(Key, value);
                        }
                    }
                    else
                    {
                        Hashtable tdata = new Hashtable();
                        tdata.Add(Key, value);
                        Data.Add(id, tdata);
                    }
                }
            }
        }
    }
}
